package com.trade.dao.listener;

import java.util.Map;

import net.esper.client.UpdateListener;
import net.esper.event.EventBean;

import org.hibernate.Session;

import com.trade.dao.impl.*;
import com.trade.dao.IEquityDAO;
import com.trade.data.EquityIntradayDataVO;
import com.trade.data.IndexIntradayDataVO;

public class PriceAvgUpdateListener extends BaseDAO implements UpdateListener {

	Session session = null;
	private static IEquityDAO equityDAO;

	public static void setEquityDAO(IEquityDAO pEquityDAO) {
		equityDAO = pEquityDAO;
	}

	public void update(EventBean[] newData, EventBean[] oldData) {
		if (newData[0].getUnderlying() instanceof Map) {
			Map<String, Object> map = (Map) newData[0].getUnderlying();
			EquityIntradayDataVO equity = new EquityIntradayDataVO();
			equity.setSymbol((String) map.get("symbol"));
			equity.setCalType((Integer) map.get("calType"));
			equity.setCalDays((Integer) map.get("calDays"));
			equity.setCalValue((Double) map.get("calValue"));

			if (equity.getCalType() == 1) {
				//System.out.println("Price Avg Received : " + equity);
				try {
					equityDAO.addEquitySMAIntradayHistoricalData(equity);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}

			/*
			 * try {equityDAO.addIntradayData((EquityIntradayDataVO)newData[0].
			 * getUnderlying()); } catch (Exception e) { e.printStackTrace(); }
			 */
		} else if (newData[0].getUnderlying() instanceof EquityIntradayDataVO) {
			EquityIntradayDataVO equity = (EquityIntradayDataVO) newData[0]
					.getUnderlying();
			// System.out.println("Equity Intraday : ");
			/*
			 * try {equityDAO.addIntradayData((EquityIntradayDataVO)newData[0].
			 * getUnderlying()); } catch (Exception e) { e.printStackTrace(); }
			 */
		} else if (newData[0].getUnderlying() instanceof IndexIntradayDataVO) {
			// System.out.println("Index Intraday : ");
		}

	}
}
